c# code for simplex method

396

c# code for simplex method -

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
    class Program
    {
        static string[] variables = { "a", "b", "c", "d", "e", "f", "g" };
        static string[] input = 
        {
            "max 4 5",
            "9 8 <= 45",
            "5 1 <= 29",
            "1 7 <= 15"
        };
        static  List<List<double>> coeifficiants1 = new List<List<double>>();
        static  List<List<double>> coeifficiants2 = new List<List<double>>();
        static List<string> operations = new List<string>();
        static string command;
        static List<double> constraints = new List<double>();
        static List<string> output = new List<string>();
        static void Main(string[] args)
        {
            ReadInput();
            CreateCommand();
        }
        static void ReadInput()
        {
            string[] fields;
            double newDouble;
            string operation = "";
            //read input lines
            foreach (string line in input)
            {
                fields = line.Split(' ');
                //check if line is command - not starting with a number
                if (double.TryParse(fields[0], out newDouble))
                {
                    Boolean firstMatrix = true;
                    List<double> matrix1row = new List<double>();
                    List<double> matrix2row = new List<double>();
                    //line contains coeifficants
                    foreach (string field in fields)
                    {
                        if (!double.TryParse(field, out newDouble))
                        {
                            operation = field;
                            firstMatrix = false;
                        }
                        else
                        {
                            newDouble = double.Parse(field);
                            if (firstMatrix == true)
                                matrix1row.Add(newDouble);
                            else
                                matrix2row.Add(newDouble);
                        }
                    }
                    coeifficiants1.Add(matrix1row);
                    coeifficiants2.Add(matrix2row);
                    operations.Add(operation);
                }
                else
                {
                    command = fields[0];
                    for (int i = 1; i < fields.Length; i++)
                    {
                        newDouble = double.Parse(fields[i]);
                        constraints.Add(newDouble);
                    }
                }
            }
        }
        static void CreateCommand()
        {
            string outputline = "";
            switch (command)
            {
                case "max":
                    outputline = "Maximize p = ";
                    break;
            }
            for(int i = 0; i < constraints.Count; i++)
            {
                if (i == 0)
                {
                    outputline += constraints[i].ToString();
                }
                else
                {
                    outputline += "+ ";
                    outputline += constraints[i].ToString();
                }
                outputline += variables[i];
             
            }
            outputline += " subject to";
            output.Add(outputline);
            
            for (int row = 0; row < coeifficiants1.Count; row++)
            {
                double[] coeifficiants = coeifficiants1[row].ToArray();
                outputline = "";
                for (int i = 0; i < coeifficiants.Length; i++)
                {
                    if (i == 0)
                    {
                        outputline += coeifficiants[i].ToString();
                    }
                    else
                    {
                        outputline += " + ";
                        outputline += coeifficiants[i].ToString();
                    }
                    outputline += variables[i];
                }
                outputline += " " + operations[row] + " "  +coeifficiants1[row][0];
                output.Add(outputline);
            }
 
        }
    }
}

Comments

Submit
0 Comments